<?php

/**
 *  For to edit emedia objects
 *
 * @copyright (c) 2012 University of Geneva
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
 * @author Laurent Opprecht laurent@opprecht.info
 */
defined('MOODLE_INTERNAL') || die;

require_once ($CFG->dirroot . '/course/moodleform_mod.php');
//require_once($CFG->dirroot . '/mod/emedia/locallib.php');
require_once($CFG->dirroot . '/mod/emedia/lib/emedia.class.php');

class mod_emedia_mod_form extends moodleform_mod
{

    function definition()
    {
        global $CFG;
        $mform = $this->_form;

        $config = get_config('emedia');

        //-------------------------------------------------------
        $mform->addElement('header', 'general', get_string('general', 'form'));
        $mform->addElement('text', 'name', get_string('name'), array('size' => '48'));
        if (!empty($CFG->formatstringstriptags))
        {
            $mform->setType('name', PARAM_TEXT);
        }
        else
        {
            $mform->setType('name', PARAM_CLEANHTML);
        }
        $mform->addHelpButton('name', 'name', 'emedia');
        $this->add_intro_editor(isset($config->requiremodintro) ? $config->requiremodintro : false);
        $mform->addHelpButton('introeditor', 'introeditor', 'emedia');

        //-------------------------------------------------------
        $mform->addElement('header', 'content', EMedia::get_string('contentheader'));
        $mform->addElement('url', 'url', EMedia::get_string('url'), array('size' => '60'), array('usefilepicker' => true));
        $mform->addRule('url', null, 'required', null, 'client');
        $mform->addHelpButton('url', 'url', 'emedia');
        //-------------------------------------------------------
        $mform->addElement('header', 'optionssection', EMedia::get_string('optionsheader'));
        $mform->addElement('text', 'size', EMedia::get_string('size'), array('size' => 3));
        $mform->addHelpButton('size', 'size', 'emedia');

        $mform->setType('size', PARAM_INT);
        $mform->setDefault('size', 300);

        //-------------------------------------------------------

        $this->standard_coursemodule_elements();
        $this->add_action_buttons();
    }

    function data_preprocessing(&$default_values)
    {
        if (empty($default_values['options']))
        {
            return;
        }
        $options = unserialize($default_values['options']);

        if (isset($options['size']))
        {
            $default_values['size'] = $options['size'];
        }
    }

    function validation($data, $files)
    {
        $errors = parent::validation($data, $files);
        unset($errors['name']); //name is not mandatory

        $url = $data['url'];
        $url = trim($url);
        $is_empty = empty($url);
        if ($is_empty)
        {
            $errors['url'] = get_string('required');
        }
        else if (!EMedia::is_url($url))
        {
            $errors['url'] = EMedia::get_string('invalidurl');
        }
        return $errors;
    }

}

